//
//  VisitorView.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/23.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
import SnapKit


class VisitorView: UIView {
    //MARK: - 设置视图信息
    func setupInfo(title: String, imageName: String?) {
        guard let imgName = imageName else {
            startAnimation()
            return
        }
        iconView.image = UIImage(named: imgName)
        messageLable.text = title
        homeIconView.hidden = true
//        maskIconView.hidden = true
        sendSubviewToBack(maskIconView)
    }
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK: 构造函数
    //使用纯代码开发的
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        setUI()
    }
    required init?(coder aDecoder: NSCoder) {
        //如果使用storyboard / xib 创建view ,程序会崩溃
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUI()
    }
    
    //MARK: - 懒加载控件
    //图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //消息文字
    private lazy var messageLable: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜")
    //注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    //登陆按钮
    lazy var loginButton: UIButton = UIButton(title: "登陆", color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
}

extension VisitorView {
    //MARK: - 设置界面
    private func setUI() {
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)


        //图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }

        //小房子
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        //消息文字
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLable.snp_left)
            make.top.equalTo(messageLable.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        //登陆按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton.snp_top)
            make.size.equalTo(registerButton.snp_size)
            make.right.equalTo(messageLable.snp_right)
        }

        //遮罩
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(registerButton.snp_bottom)
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
    }
}








